var login_popup = null
var login_users = new Array()
var login_form_sending = false

function loginShow()
{
	login_popup = new Popup(450, 533);
	login_popup.show(null)
	ajax.sendAsync("./action/user/login.php5?action=show&u="+new Date().getTime(), function(datas)
	{
		ajax.eval(datas)
	})
}

var login_last_selected_user = null
var login_selected_account = "tt";
function loginSelectUser(event)
{
	var form = document.getElementById("login_form")
	if(login_form_sending)
	{
		return
	}
	if(window.event)
	{
		target = window.event.srcElement
	}
	else
	{
		target = event.target
	}
	
	var new_account = null;
	while((target.tagName != "LI" && target != null) || target.getAttribute("user")==null)
	{
		if(target.tagName == "LI" && target.getAttribute("account")!=null)
		{
			new_account = target.getAttribute("account")
		}
		target = target.parentNode;
	}
	if(target==null || target.getAttribute("user")==null)
	{
		return
	}
	var user_id = target.getAttribute("user");
	
	
	if(user_id == "other" && new_account!=null)
	{
		login_selected_account = new_account
		document.getElementById("login_user_other").setAttribute("account", login_selected_account);
		if(window.event)
		{
			document.getElementById("login_user_other").className+=""
		}
	}

	
	if(login_last_selected_user!=null)
	{
		if(login_last_selected_user==user_id)
		{
			if(user_id=="other")
			{
					if(new_account != null)
					{
						focusInput(document.getElementById("login_user_"+login_last_selected_user+"_"+new_account))
					}
			}
		}
		document.getElementById("login_user_"+login_last_selected_user).removeAttribute("selected")
	}
	login_last_selected_user = user_id
	document.getElementById("login_user_"+user_id).setAttribute("selected", true)
	if(login_last_selected_user != "other" || new_account != null)
	{
		if(login_last_selected_user == "other")
		{
			focusInput(document.getElementById("login_user_"+login_last_selected_user+"_"+new_account))
		}
		else
		{
			focusInput(document.getElementById("login_user_"+login_last_selected_user));
		}
	}
}

function loginSent()
{
	document.getElementById("login_warning_icon").style.visibility="hidden"
	var form = document.getElementById("login_form")
	if(login_form_sending)
	{
		return
	}
	if(login_last_selected_user==null)
	{
		return;
	}
	login_form_sending = true
	disabledForm(form, true)
	document.getElementById("login_submit").disabled = true;
	
	var datas = new Array();
	if(login_last_selected_user=="other")
	{
		datas['account'] = login_selected_account
		switch(login_selected_account)
		{
			case 'tt':
				datas["login"] = document.getElementById("login_user_txt_login_other_tt").value;
				datas["password"] = document.getElementById("login_user_txt_password_other_tt").value;
				break;
			case 'gmail':
				datas["login"] = document.getElementById("login_user_txt_login_other_gmail").value;
				datas["password"] = document.getElementById("login_user_txt_password_other_gmail").value;
				break;
		}
	}
	else
	{
		datas['account'] = document.getElementById("login_user_"+login_last_selected_user).getAttribute("account");
		
		switch(datas['account'])
		{
			case 'tt':
				datas["login"] = document.getElementById("login_user_txt_login_"+login_last_selected_user+"_tt").getAttribute("login");
				datas["password"] = document.getElementById("login_user_txt_password_"+login_last_selected_user+"_tt").value;
				break;
			case 'gmail':
				datas["login"] = document.getElementById("login_user_txt_login_"+login_last_selected_user+"_gmail").getAttribute("login");
				datas["password"] = document.getElementById("login_user_txt_password_"+login_last_selected_user+"_gmail").value;
				break;
		}
	}
	var account = datas['account']
	var selected_user = login_last_selected_user;
	var selected_login = datas['login'];
	
	ajax.sendAsync("./action/user/login.php5?action=login&u="+new Date().getTime(), function(datas)
	{
		clearForm(form)
		disabledForm(form, false)
		document.getElementById("login_submit").disabled = false;
		if(selected_user == 'other')
		{
			switch(account)
			{
				case 'tt':
					document.getElementById("login_user_txt_login_"+selected_user+"_tt").value = selected_login
					if(selected_login!='')
					{
						document.getElementById("login_user_txt_login_"+selected_user+"_tt").removeAttribute('empty')
					}
					try
					{
						document.getElementById("login_user_txt_password_"+selected_user+"_tt").focus()
					}
					catch(e){}
					break;
				case 'gmail':
					document.getElementById("login_user_txt_login_"+selected_user+"_gmail").value = selected_login
					if(selected_login!='')
					{
						document.getElementById("login_user_txt_login_"+selected_user+"_gmail").removeAttribute('empty')
					}
					break;
			}
		}
		switch(account)
		{
			case 'tt':
				try
				{
					document.getElementById("login_user_txt_password_"+selected_user+"_tt").focus();
				}
				catch(e){}
				break;
			case 'gmail':
				try
				{
					document.getElementById("login_user_txt_password_"+selected_user+"_gmail").focus()
				}
				catch(e){}
				break;
		}
		ajax.eval(datas)
		login_form_sending = false
	}, datas)
}

function loginError(message, tips, show)
{
	if(show==null) show = false
	if(tips==null)
	{
		document.getElementById("login_warning").innerHTML = message
	}
	else
	{
		document.getElementById("login_warning").innerHTML = '<div class="tooltips">'+message+'<span class="tip"><div class="tip_body">'+tips+'</div></span></div>';
	}
	if(show)
	{
		loginShowError()
	}
}
function loginShowError()
{
	if(document.getElementById("login_warning").innerHTML != "")
	{
		document.getElementById("login_warning_icon").style.visibility="visible"
	}
}
function loginHideError()
{
	document.getElementById("login_warning").innerHTML=""
	document.getElementById("login_warning_icon").style.visibility="hidden"
}


function loginLogout()
{
	ajax.sendAsync("./action/user/login.php5?action=logout&u="+new Date().getTime(), function(datas)
	{
		ajax.eval(datas)
	})
}

function loginInit()
{
	login_selected_account = "tt";
	document.getElementById("login_user_"+login_last_selected_user).setAttribute("selected", true);
	focusInput(document.getElementById("login_user_"+login_last_selected_user))


	if (window.HTMLElement)
	{
		document.getElementById("login_user_other").addEventListener('DOMMouseScroll', function(e)
		{
			loginWheel(-e.detail/3)
		}, false);
	}
	else
	{
		document.getElementById("login_user_other").onmousewheel = function()
		{
			if(window.opera)
			{
				loginWheel(-event.wheelDelta/120)
			}
			else
			{
				loginWheel(event.wheelDelta/120)
			}
		}
	}
}

function loginWheel(data)
{
	data = -data
	var accounts = new Array('tt', 'wll', 'gmail', 'yahoo');
	var pos = accounts.indexOf(login_selected_account)
	if(pos<0)
	{
		alert('error')
	}
	if(data<0)
	{
		while(pos>0)
		{
			pos--;
			if(document.getElementById('login_account_'+accounts[pos])!=null)
			{
				login_selected_account = accounts[pos]
				document.getElementById("login_user_other").setAttribute("account", login_selected_account);
				break
			}
		}
	}
	else
	{
		while(pos<3)
		{
			pos++;
			if(document.getElementById('login_account_'+accounts[pos])!=null)
			{
				login_selected_account = accounts[pos]
				document.getElementById("login_user_other").setAttribute("account", login_selected_account);
				break
			}
		}
	}
	
	if(window.event)
	{
		document.getElementById("login_user_other").className+=""
	}
}
